package com.cap.dreamcircle.View;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.cap.dreamcircle.Base.BaseFragment;
import com.cap.dreamcircle.Model.Bean.AdsArticleBean;
import com.cap.dreamcircle.Model.Bean.BaseBean;
import com.cap.dreamcircle.Model.Bean.MainNewsEntity;
import com.cap.dreamcircle.Model.IDreamModel;
import com.cap.dreamcircle.Model.Impl.DreamModelImpl;
import com.cap.dreamcircle.R;
import com.cap.dreamcircle.Utils.CommonSubscriber;
import com.cap.dreamcircle.Utils.KeyCounterUtils;
import com.cap.dreamcircle.Utils.ShareDialogUtils;
import com.cap.dreamcircle.Utils.ToastUtils;
import com.cap.dreamcircle.View.Adapter.AdapterMainNews;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by liucxu on 2017/6/21.
 */

public class FragmentMain extends BaseFragment {

    @BindView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout swipe_refresh_layout;
    @BindView(R.id.list_news)
    ListView list_news;

    private View header_list_view;
    private AdapterMainNews adapter;
    private List<MainNewsEntity> news = new ArrayList<>();

    private ImageView imv_select_date;
    private ImageView imv_record_dream;
    private ImageView imv_share_app;
    private RelativeLayout layout_to_resolve_dream;
    private IDreamModel model;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_main;
    }

    @Override
    public void setupView() {
        ButterKnife.bind(this, mContextView);
        model = new DreamModelImpl();
        adapter = new AdapterMainNews(news, getActivity());
        header_list_view = View.inflate(getActivity(), R.layout.header_main, null);
        imv_record_dream = (ImageView) header_list_view.findViewById(R.id.imv_record_dream);
        imv_share_app=(ImageView) header_list_view.findViewById(R.id.imv_share_app);
        layout_to_resolve_dream=(RelativeLayout)header_list_view.findViewById(R.id.layout_to_resolve_dream);
        imv_share_app.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShareDialogUtils.init().share(getActivity(),"www.qq.com");
            }
        });
        layout_to_resolve_dream.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                KeyCounterUtils.SendKeyCounter("3079c93e7649482992719e21d1963444");
                Intent intent = new Intent(getActivity(), DreamLabelActivity.class);
                startActivity(intent);
            }
        });
        imv_select_date = (ImageView) header_list_view.findViewById(R.id.imv_select_date);
        imv_record_dream.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                KeyCounterUtils.SendKeyCounter("93e71143d9cb495c828998d50bb9d499");
                Intent intent = new Intent(getActivity(), RecordDreamTitleActivcity.class);
                startActivity(intent);
            }
        });
        imv_select_date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                KeyCounterUtils.SendKeyCounter("787723d2aa29445b96d650611809bec0");
                Intent intent = new Intent(getActivity(), CalenderDreamsActivity.class);
                startActivity(intent);
            }
        });
        if (list_news.getHeaderViewsCount() == 0) {
            list_news.addHeaderView(header_list_view);
        }
        list_news.setAdapter(adapter);
        swipe_refresh_layout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                requestMainPageAds();
            }
        });
    }

    @Override
    public void setupData() {
    }

    @Override
    public void onResume() {
        super.onResume();
        requestMainPageAds();
    }

    private void requestMainPageAds() {
        model.RequestMainPageAds().subscribe(new CommonSubscriber<AdsArticleBean>() {
            @Override
            public void onSuccess(AdsArticleBean response) {
                swipe_refresh_layout.setRefreshing(false);
                adapter.setDatas(response.getAdArray());
            }

            @Override
            public void onFailure(BaseBean response) {
                swipe_refresh_layout.setRefreshing(false);
                ToastUtils.showShortToast(getContext(),response.getDesc());
            }
        });
    }
}
